package me.zhengjie.modules.biz.utils;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import me.zhengjie.config.AppConfig;
import me.zhengjie.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.swing.filechooser.FileSystemView;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class QRCodeUtil {
    private static final Logger log = LoggerFactory.getLogger(QRCodeUtil.class);

    private static final String path = "/tmp/xxx.png";

    //CODE_WIDTH：二维码宽度，单位像素
    private static final int CODE_WIDTH = 400;
    //CODE_HEIGHT：二维码高度，单位像素
    private static final int CODE_HEIGHT = 400;
    //FRONT_COLOR：二维码前景色，0x000000 表示黑色
    private static final int FRONT_COLOR = 0x000000;
    //BACKGROUND_COLOR：二维码背景色，0xFFFFFF 表示白色
    //演示用 16 进制表示，和前端页面 CSS 的取色是一样的，注意前后景颜色应该对比明显，如常见的黑白
    private static final int BACKGROUND_COLOR = 0xFFFFFF;

    public static void createCodeToFile(String content) {
        try {
            File codeImgFileSaveDir = new File("C:\\Users\\18715\\Desktop");
            String fileName = "xxx.png";
            content = content.trim();
            //核心代码-生成二维码
            BufferedImage bufferedImage = getBufferedImage(content);

            File codeImgFile = new File(codeImgFileSaveDir, fileName);
            ImageIO.write(bufferedImage, "png", codeImgFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
//
//    public static void show(HttpServletResponse response){
//        createCodeToFile("123");
//        addTextShow("啊啊啊",path);
//        downloadPicture("蒋丹",response);
//    }


    public static void showListCodeToFile(String content, String path, String fileName) {
        try {
            File codeImgFileSaveDir = new File(path);
//            String fileName = "xxx.png";
            content = content.trim();
            //核心代码-生成二维码
            BufferedImage bufferedImage = getBufferedImage(content);

            File codeImgFile = new File(codeImgFileSaveDir, fileName);
            ImageIO.write(bufferedImage, "png", codeImgFile);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    public static void show(HttpServletResponse response){
//        createCodeToFile("123");
//        addTextShow("啊啊啊",path);
//        downloadPicture("蒋丹",response);
//    }

    /**
     * 生成二维码并输出到输出流, 通常用于输出到网页上进行显示，输出到网页与输出到磁盘上的文件中，区别在于最后一句 ImageIO.write
     * write(RenderedImage im,String formatName,File output)：写到文件中
     * write(RenderedImage im,String formatName,OutputStream output)：输出到输出流中
     *
     * @param content      ：二维码内容
     * @param outputStream ：输出流，比如 HttpServletResponse 的 getOutputStream
     */
    public static void createCodeToOutputStream(String content, OutputStream outputStream) {
        try {
            if (StringUtils.isBlank(content)) {
                return;
            }
            content = content.trim();
            //核心代码-生成二维码
            BufferedImage bufferedImage = getBufferedImage(content);

            //区别就是这一句，输出到输出流中，如果第三个参数是 File，则输出到文件中
            ImageIO.write(bufferedImage, "png", outputStream);

//            log.info("二维码图片生成到输出流成功...");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //核心代码-生成二维码
    private static BufferedImage getBufferedImage(String content) throws WriterException {

        //com.google.zxing.EncodeHintType：编码提示类型,枚举类型
        Map<EncodeHintType, Object> hints = new HashMap();

        //EncodeHintType.CHARACTER_SET：设置字符编码类型
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");

        //EncodeHintType.ERROR_CORRECTION：设置误差校正
        //ErrorCorrectionLevel：误差校正等级，L = ~7% correction、M = ~15% correction、Q = ~25% correction、H = ~30% correction
        //不设置时，默认为 L 等级，等级不一样，生成的图案不同，但扫描的结果是一样的
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);

        //EncodeHintType.MARGIN：设置二维码边距，单位像素，值越小，二维码距离四周越近
        hints.put(EncodeHintType.MARGIN, 1);

        MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
        BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, CODE_WIDTH, CODE_HEIGHT, hints);
        BufferedImage bufferedImage = new BufferedImage(CODE_WIDTH, CODE_HEIGHT, BufferedImage.TYPE_INT_BGR);
        for (int x = 0; x < CODE_WIDTH; x++) {
            for (int y = 0; y < CODE_HEIGHT; y++) {
                bufferedImage.setRGB(x, y, bitMatrix.get(x, y) ? FRONT_COLOR : BACKGROUND_COLOR);
            }
        }
        return bufferedImage;
    }

    public static void addTextShow(ArrayList<String> descList, String path, HashMap<String, Integer> config, Integer type) {
        BufferedImage src = null;
        try {
            src = ImageIO.read(new File(path));
        } catch (IOException e) {
            e.printStackTrace();
        }
//        System.out.println(src.getHeight());
//        System.out.println(src.getWidth());
        if (config.get("equipment_qrcode_size") == 50 && config.get("equipment_qrcode_show_text") == 1 && type == 1
                || config.get("substance_qrcode_size") == 50 && config.get("substance_qrcode_show_text") == 1 && type == 2
                || config.get("reagent_qrcode_size") == 50 && config.get("reagent_qrcode_show_text") == 1 && type == 3) {
            BufferedImage b = new BufferedImage(src.getWidth() + 500, src.getHeight() + 300, src.getType());
            Graphics2D g2d = b.createGraphics();
            g2d.setColor(Color.WHITE);
            g2d.fillRect(0, 0, b.getWidth(), b.getHeight());
            g2d.drawImage(src, 500, 170, null);
            g2d.dispose();
            try {
                ImageIO.write(b, "png", new File(path));//写出文件
            } catch (IOException e) {
                e.printStackTrace();
            }

            BufferedImage image = null;
            try {
                image = ImageIO.read(new File(path));
            } catch (IOException e) {
                e.printStackTrace();
            }

            Graphics2D pen = image.createGraphics();
            // 设置画笔颜色为白色
            // pen.setColor(Color.WHITE);
            pen.setColor(new Color(0, 0, 0, 255));
            // 设置画笔字体样式为微软雅黑，斜体，文字大小为20px
            Font font = new Font("黑体", Font.PLAIN, 50);
            pen.setFont(font);
            BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
            FontMetrics fm = img.getGraphics().getFontMetrics(font);
//        int width = fm.stringWidth(ChineseName);
            // 写上水印文字和坐标
            if (descList.size() != 1) {
                if (descList.get(0).length() < 9) {
                    pen.drawString(descList.get(0), (image.getWidth()) - 750, image.getHeight() - 470);
                    pen.drawString(descList.get(1), (image.getWidth()) - 750, image.getHeight() - 320);
                    pen.drawString(descList.get(2), (image.getWidth()) - 750, image.getHeight() - 170);
//                pen.drawString(descList.get(3), (image.getWidth())-750, image.getHeight()-190);
                } else if (descList.get(0).length() < 17) {
                    String nameList = descList.get(0);
                    String start = nameList.substring(0, 8);
                    String end = nameList.substring(8);
                    pen.drawString(start, (image.getWidth()) - 750, image.getHeight() - 500);
                    pen.drawString(end, (image.getWidth()) - 750, image.getHeight() - 450);
                    pen.drawString(descList.get(1), (image.getWidth()) - 750, image.getHeight() - 305);
                    pen.drawString(descList.get(2), (image.getWidth()) - 750, image.getHeight() - 160);
//                pen.drawString(descList.get(3), (image.getWidth())-750, image.getHeight()-160);
                } else if (descList.get(0).length() < 25) {
                    String nameList = descList.get(0);
                    String start = nameList.substring(0, 8);
                    String middle = nameList.substring(8, 16);
                    String end = nameList.substring(16);
                    pen.drawString(start, (image.getWidth()) - 750, image.getHeight() - 500);
                    pen.drawString(middle, (image.getWidth()) - 750, image.getHeight() - 450);
                    pen.drawString(end, (image.getWidth()) - 750, image.getHeight() - 400);
                    pen.drawString(descList.get(1), (image.getWidth()) - 750, image.getHeight() - 280);
                    pen.drawString(descList.get(2), (image.getWidth()) - 750, image.getHeight() - 160);
                } else if (descList.get(0).length() < 32) {
                    String nameList = descList.get(0);
                    String start = nameList.substring(0, 8);
                    String middle1 = nameList.substring(8, 16);
                    String middle2 = nameList.substring(16, 24);
                    String end = nameList.substring(24);
                    pen.drawString(start, (image.getWidth()) - 750, image.getHeight() - 500);
                    pen.drawString(middle1, (image.getWidth()) - 750, image.getHeight() - 450);
                    pen.drawString(middle2, (image.getWidth()) - 750, image.getHeight() - 400);
                    pen.drawString(end, (image.getWidth()) - 750, image.getHeight() - 350);
                    pen.drawString(descList.get(1), (image.getWidth()) - 750, image.getHeight() - 255);
                    pen.drawString(descList.get(2), (image.getWidth()) - 750, image.getHeight() - 160);
                } else {
                    String nameList = descList.get(0);
                    String start = nameList.substring(0, 8);
                    String middle1 = nameList.substring(8, 16);
                    String middle2 = nameList.substring(16, 24);
                    String middle3 = nameList.substring(24, 32);
                    String end = nameList.substring(32);
                    pen.drawString(start, (image.getWidth()) - 750, image.getHeight() - 500);
                    pen.drawString(middle1, (image.getWidth()) - 750, image.getHeight() - 450);
                    pen.drawString(middle2, (image.getWidth()) - 750, image.getHeight() - 400);
                    pen.drawString(middle3, (image.getWidth()) - 750, image.getHeight() - 350);
                    pen.drawString(end, (image.getWidth()) - 750, image.getHeight() - 300);
                    pen.drawString(descList.get(1), (image.getWidth()) - 750, image.getHeight() - 230);
                    pen.drawString(descList.get(2), (image.getWidth()) - 750, image.getHeight() - 160);
                }


            } else {
                pen.drawString(descList.get(0), (image.getWidth()) - 750, image.getHeight() - 300);
            }
//        pen.drawString(ChineseName, (image.getWidth()-width)/2, image.getHeight()-20);

            // 创建新图片文件
            File file = new File(path);
            // 将处理好的图片数据写入到新图片文件中
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(file);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            try {
                ImageIO.write(image, "png", fos);
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                src = ImageIO.read(new File(path));
            } catch (IOException e) {
                e.printStackTrace();
            }
            b = new BufferedImage(400, 320, src.getType());
            g2d = b.createGraphics();
            g2d.drawImage(src, 0, 0, 400, 320, null);
            g2d.dispose();
            g2d.setComposite(AlphaComposite.Src);
            g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            try {
                ImageIO.write(b, "png", new File(path));//写出文件
            } catch (IOException e) {
                e.printStackTrace();
            }

        } else if (config.get("equipment_qrcode_size") == 50 && config.get("equipment_qrcode_show_text") == 0 && type == 1
                || config.get("substance_qrcode_size") == 50 && config.get("substance_qrcode_show_text") == 0 && type == 2
                || config.get("reagent_qrcode_size") == 50 && config.get("reagent_qrcode_show_text") == 0 && type == 3) {
            BufferedImage b = new BufferedImage(src.getWidth() + 100, src.getHeight() + 100, src.getType());
            Graphics2D g2d = b.createGraphics();
            g2d.setColor(Color.WHITE);
            g2d.fillRect(0, 0, b.getWidth(), b.getHeight());
            g2d.drawImage(src, 100, 20, null);
            g2d.dispose();
            try {
                ImageIO.write(b, "png", new File(path));//写出文件
            } catch (IOException e) {
                e.printStackTrace();
            }

            BufferedImage image = null;
            try {
                image = ImageIO.read(new File(path));
            } catch (IOException e) {
                e.printStackTrace();
            }
//        int width = fm.stringWidth(ChineseName);
            // 写上水印文字和坐标
//        pen.drawString(ChineseName, (image.getWidth()-width)/2, image.getHeight()-20);

            // 创建新图片文件
            File file = new File(path);
            // 将处理好的图片数据写入到新图片文件中
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(file);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            try {
                ImageIO.write(image, "png", fos);
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                src = ImageIO.read(new File(path));
            } catch (IOException e) {
                e.printStackTrace();
            }
            b = new BufferedImage(400, 320, src.getType());
            g2d = b.createGraphics();
            g2d.drawImage(src, 0, 0, 400, 320, null);
            g2d.dispose();
            g2d.setComposite(AlphaComposite.Src);
            g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            try {
                ImageIO.write(b, "png", new File(path));//写出文件
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else if (config.get("equipment_qrcode_size") == 30 && config.get("equipment_qrcode_show_text") == 1 && type == 1
                || config.get("substance_qrcode_size") == 30 && config.get("substance_qrcode_show_text") == 1 && type == 2
                || config.get("reagent_qrcode_size") == 30 && config.get("reagent_qrcode_show_text") == 1 && type == 3) {
            BufferedImage b = new BufferedImage(src.getWidth() + 350, src.getHeight() + 250, src.getType());
            Graphics2D g2d = b.createGraphics();
            g2d.setColor(Color.WHITE);
            g2d.fillRect(0, 0, b.getWidth(), b.getHeight());
            g2d.drawImage(src, 335, 145, null);
            g2d.dispose();
            try {
                ImageIO.write(b, "png", new File(path));//写出文件
            } catch (IOException e) {
                e.printStackTrace();
            }

            BufferedImage image = null;
            try {
                image = ImageIO.read(new File(path));
            } catch (IOException e) {
                e.printStackTrace();
            }

            Graphics2D pen = image.createGraphics();
            // 设置画笔颜色为白色
            // pen.setColor(Color.WHITE);
            pen.setColor(new Color(0, 0, 0, 255));
            // 设置画笔字体样式为微软雅黑，斜体，文字大小为20px
            Font font = new Font("黑体", Font.PLAIN, 50);
            pen.setFont(font);
            BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
            FontMetrics fm = img.getGraphics().getFontMetrics(font);
//        int width = fm.stringWidth(ChineseName);
            // 写上水印文字和坐标
            if (descList.size() != 1) {
                if (descList.get(0).length() < 7) {
                    pen.drawString(descList.get(0), (image.getWidth()) - 745, image.getHeight() - 450);
                    pen.drawString(descList.get(1), (image.getWidth()) - 745, image.getHeight() - 300);
                    pen.drawString(descList.get(2), (image.getWidth()) - 745, image.getHeight() - 150);
//                pen.drawString(descList.get(3), (image.getWidth())-850, image.getHeight()-190);
                } else if (descList.get(0).length() < 13) {
                    String nameList = descList.get(0);
                    String start = nameList.substring(0, 6);
                    String end = nameList.substring(6);
                    pen.drawString(start, (image.getWidth()) - 745, image.getHeight() - 450);
                    pen.drawString(end, (image.getWidth()) - 745, image.getHeight() - 400);
                    pen.drawString(descList.get(1), (image.getWidth()) - 745, image.getHeight() - 260);
                    pen.drawString(descList.get(2), (image.getWidth()) - 745, image.getHeight() - 110);
//                pen.drawString(descList.get(3), (image.getWidth())-850, image.getHeight()-160);
                } else if (descList.get(0).length() < 19) {
                    String nameList = descList.get(0);
                    String start = nameList.substring(0, 6);
                    String middle = nameList.substring(6, 12);
                    String end = nameList.substring(12);
                    pen.drawString(start, (image.getWidth()) - 745, image.getHeight() - 450);
                    pen.drawString(middle, (image.getWidth()) - 745, image.getHeight() - 400);
                    pen.drawString(end, (image.getWidth()) - 745, image.getHeight() - 350);
                    pen.drawString(descList.get(1), (image.getWidth()) - 745, image.getHeight() - 310);
                    pen.drawString(descList.get(2), (image.getWidth()) - 745, image.getHeight() - 160);
//                pen.drawString(descList.get(3), (image.getWidth())-850, image.getHeight()-160);
                } else if (descList.get(0).length() < 25) {
                    String nameList = descList.get(0);
                    String start = nameList.substring(0, 6);
                    String middle1 = nameList.substring(6, 12);
                    String middle2 = nameList.substring(12, 18);
                    String end = nameList.substring(18);
                    pen.drawString(start, (image.getWidth()) - 745, image.getHeight() - 450);
                    pen.drawString(middle1, (image.getWidth()) - 745, image.getHeight() - 400);
                    pen.drawString(middle2, (image.getWidth()) - 745, image.getHeight() - 350);
                    pen.drawString(end, (image.getWidth()) - 745, image.getHeight() - 300);
                    pen.drawString(descList.get(1), (image.getWidth()) - 745, image.getHeight() - 220);
                    pen.drawString(descList.get(2), (image.getWidth()) - 745, image.getHeight() - 140);
                } else {
                    String nameList = descList.get(0);
                    String start = nameList.substring(0, 6);
                    String middle1 = nameList.substring(6, 12);
                    String middle2 = nameList.substring(12, 18);
                    String middle3 = nameList.substring(18, 24);
                    String end = nameList.substring(24);
                    pen.drawString(start, (image.getWidth()) - 745, image.getHeight() - 450);
                    pen.drawString(middle1, (image.getWidth()) - 745, image.getHeight() - 400);
                    pen.drawString(middle2, (image.getWidth()) - 745, image.getHeight() - 350);
                    pen.drawString(middle3, (image.getWidth()) - 745, image.getHeight() - 300);
                    pen.drawString(end, (image.getWidth()) - 745, image.getHeight() - 250);
                    pen.drawString(descList.get(1), (image.getWidth()) - 745, image.getHeight() - 195);
                    pen.drawString(descList.get(2), (image.getWidth()) - 745, image.getHeight() - 140);
                }

            } else {
                pen.drawString(descList.get(0), (image.getWidth()) - 745, image.getHeight() - 300);
            }
//        pen.drawString(ChineseName, (image.getWidth()-width)/2, image.getHeight()-20);

            // 创建新图片文件
            File file = new File(path);
            // 将处理好的图片数据写入到新图片文件中
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(file);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            try {
                ImageIO.write(image, "png", fos);
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                src = ImageIO.read(new File(path));
            } catch (IOException e) {
                e.printStackTrace();
            }
            b = new BufferedImage(200, 160, src.getType());
            g2d = b.createGraphics();
            g2d.drawImage(src, 0, 0, 200, 160, null);
            g2d.dispose();
            g2d.setComposite(AlphaComposite.Src);
            g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            try {
                ImageIO.write(b, "png", new File(path));//写出文件
            } catch (IOException e) {
                e.printStackTrace();
            }

        } else if (config.get("equipment_qrcode_size") == 30 && config.get("equipment_qrcode_show_text") == 0 && type == 1
                || config.get("substance_qrcode_size") == 30 && config.get("substance_qrcode_show_text") == 0 && type == 2
                || config.get("reagent_qrcode_size") == 30 && config.get("reagent_qrcode_show_text") == 0 && type == 3) {
            BufferedImage b = new BufferedImage(src.getWidth() + 100, src.getHeight() + 100, src.getType());
            Graphics2D g2d = b.createGraphics();
            g2d.setColor(Color.WHITE);
            g2d.fillRect(0, 0, b.getWidth(), b.getHeight());
            g2d.drawImage(src, 50, 0, null);
            g2d.dispose();
            try {
                ImageIO.write(b, "png", new File(path));//写出文件
            } catch (IOException e) {
                e.printStackTrace();
            }

            BufferedImage image = null;
            try {
                image = ImageIO.read(new File(path));
            } catch (IOException e) {
                e.printStackTrace();
            }
//        int width = fm.stringWidth(ChineseName);
            // 写上水印文字和坐标
//        pen.drawString(ChineseName, (image.getWidth()-width)/2, image.getHeight()-20);

            // 创建新图片文件
            File file = new File(path);
            // 将处理好的图片数据写入到新图片文件中
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(file);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            try {
                ImageIO.write(image, "png", fos);
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                src = ImageIO.read(new File(path));
            } catch (IOException e) {
                e.printStackTrace();
            }
            b = new BufferedImage(220, 180, src.getType());
            g2d = b.createGraphics();
            g2d.drawImage(src, 0, 0, 200, 160, null);
            g2d.dispose();
            g2d.setComposite(AlphaComposite.Src);
            g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            try {
                ImageIO.write(b, "png", new File(path));//写出文件
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

    public static void showPicture(String name, String path, HttpServletResponse response) {
        try {
            File file = new File(path);
            long size = file.length() / 1024;

            FileInputStream fileInputStream = new FileInputStream(new File(path));
            //输出流，通过输出流将文件写回浏览器
            ServletOutputStream outputStream = response.getOutputStream();
            response.setContentType("image/png");
            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len = fileInputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, len);
                outputStream.flush();
            }
            //关闭资源
            outputStream.close();
            fileInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void downloadPicture(String name, String path, HttpServletResponse response) {
        try {
            File file = new File(path);
//            long size = file.length() / 1024;

            FileInputStream fileInputStream = new FileInputStream(new File(path));
            //输出流，通过输出流将文件写回浏览器
            ServletOutputStream outputStream = response.getOutputStream();
            response.setContentType("image/png");
            response.setHeader("Content-Description", "File Transfer");
//            response.setCharacterEncoding("UTF-8");
//            response.setContentType("text/html;charset=UTF-8");
            name = name + ".png";
            String fileName = new String(name.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
            response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
            response.setHeader("Content-Length", file.length() + "");
            response.setHeader("Content-Type", "application/octet-stream");
            response.setHeader("Expires", "0");
            response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
            response.setHeader("Content-Transfer-Encoding", "binary");

            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len = fileInputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, len);
                outputStream.flush();
            }
            //关闭资源
            outputStream.close();
            fileInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static final Object obj = new Object();

    public static void showQRCode(String msg, String name, Integer serialNumber, ArrayList<String> descList, HashMap<String, Integer> config, Integer type, HttpServletResponse response) {
        String path = AppConfig.baseBarCodePath;
//        String path = "C:\\Users\\18715\\Desktop";
        String fileName = "QRCode" + serialNumber + ".png";
        synchronized (obj) {
            // 临界区
            showListCodeToFile(msg, path, fileName);
            addTextShow(descList, path.concat(fileName), config, type);
            showPicture(name, path.concat(fileName), response);
        }


    }

    public static void printQRCode(String msg, String name, ArrayList<String> descList, HashMap<String, Integer> config, Integer type, HttpServletResponse response) {
        String path = AppConfig.baseBarCodePath;
//        String path = "C:\\Users\\18715\\Desktop";
        String fileName = "qrcode.png";

        synchronized (obj) {
            // 临界区
            showListCodeToFile(msg, path, fileName);
            addTextShow(descList, path.concat(fileName), config, type);
            downloadPicture(name, path.concat(fileName), response);
        }


    }

    public static void bulkPrintBarCode(String msg, String name, ArrayList<String> descList, String path, String fileName) {
        showListCodeToFile(msg, path, fileName);
//        addTextShow(descList,path.concat("\\").concat(fileName));
    }
}
